package helperx

import (
	"time"
)

// IsSameDay 2个时间是否为同一天
func IsSameDay(t1, t2 time.Time) bool {
	y1, m1, d1 := t1.Date()
	y2, m2, d2 := t2.Date()

	return y1 == y2 && m1 == m2 && d1 == d2
}

// IsSameMonth 2个时间是否为同一月
func IsSameMonth(t1, t2 time.Time) bool {
	y1, m1, _ := t1.Date()
	y2, m2, _ := t2.Date()

	return y1 == y2 && m1 == m2
}

// IsSameYear 2个时间是否为同一年
func IsSameYear(t1, t2 time.Time) bool {
	return t1.Year() == t2.Year()
}

// GetAgeFromBirthday 获取周岁年龄
func GetAgeFromBirthday(birth time.Time) (age int) {
	y := time.Now().Year() - birth.Year()
	m := int(time.Now().Month()) - int(birth.Month())
	d := time.Now().Day() - birth.Day()

	age = y

	// 对比年份
	if y <= 0 {
		return 0
	}

	// 对比月份
	if m < 0 && y > 0 {
		return age - 1
	}

	// 对比日期
	if d < 0 && m <= 0 && y >= 0 {
		return age - 1
	}

	return
}
